package cc.rengu.igas.bsps.biz.service.impl;

import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.facade.MchntManageService;
import cc.rengu.igas.bsps.facade.request.*;
import cc.rengu.igas.bsps.facade.response.*;
import cc.rengu.igas.bsps.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 商户管理服务实现类
 * Created by 王成 on 2018/7/19.
 */
public class MchntManageServiceImpl implements MchntManageService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<MchntApplyResponse> mchntApply(MchntApplyRequest mchntApplyRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntApplyResponse> bspResult = new Result<>();
        MchntApplyResponse mchntApplyResponse = new MchntApplyResponse();
        mchntApplyResponse.setHeader(mchntApplyRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntApplyRequest, MchntApplyResponse.class);
            mchntApplyResponse = (MchntApplyResponse) dubboResult.getResult();
            mchntApplyResponse.setRespCode(dubboResult.getRespCode());
            mchntApplyResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户入驻申请失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntApplyResponse.setRespCode(((BizException) e).getCode());
                mchntApplyResponse.setRespDesc(e.getMessage());
            } else {
                mchntApplyResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntApplyResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntApplyResponse);
        return bspResult;
    }

    @Override
    public Result<MchntBindAuthResponse> mchntBindAuth(MchntBindAuthRequest mchntBindAuthRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntBindAuthResponse> bspResult = new Result<>();
        MchntBindAuthResponse mchntBindAuthResponse = new MchntBindAuthResponse();
        mchntBindAuthResponse.setHeader(mchntBindAuthRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntBindAuthRequest, MchntBindAuthResponse.class);
            mchntBindAuthResponse = (MchntBindAuthResponse) dubboResult.getResult();
            mchntBindAuthResponse.setRespCode(dubboResult.getRespCode());
            mchntBindAuthResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户绑定认证失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntBindAuthResponse.setRespCode(((BizException) e).getCode());
                mchntBindAuthResponse.setRespDesc(e.getMessage());
            } else {
                mchntBindAuthResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntBindAuthResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntBindAuthResponse);
        return bspResult;
    }

    @Override
    public Result<MchntBindRegisterResponse> mchntBindRegister(MchntBindRegisterRequest mchntBindRegisterRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntBindRegisterResponse> bspResult = new Result<>();
        MchntBindRegisterResponse mchntBindRegisterResponse = new MchntBindRegisterResponse();
        mchntBindRegisterResponse.setHeader(mchntBindRegisterRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntBindRegisterRequest, MchntBindRegisterResponse.class);
            mchntBindRegisterResponse = (MchntBindRegisterResponse) dubboResult.getResult();
            mchntBindRegisterResponse.setRespCode(dubboResult.getRespCode());
            mchntBindRegisterResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户绑定注册失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntBindRegisterResponse.setRespCode(((BizException) e).getCode());
                mchntBindRegisterResponse.setRespDesc(e.getMessage());
            } else {
                mchntBindRegisterResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntBindRegisterResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntBindRegisterResponse);
        return bspResult;
    }

    @Override
    public Result<MchntUnBindResponse> mchntUnBind(MchntUnBindRequest mchntUnBindRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntUnBindResponse> bspResult = new Result<>();
        MchntUnBindResponse mchntUnBindResponse = new MchntUnBindResponse();
        mchntUnBindResponse.setHeader(mchntUnBindRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntUnBindRequest, MchntUnBindResponse.class);
            mchntUnBindResponse = (MchntUnBindResponse) dubboResult.getResult();
            mchntUnBindResponse.setRespCode(dubboResult.getRespCode());
            mchntUnBindResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户解绑失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntUnBindResponse.setRespCode(((BizException) e).getCode());
                mchntUnBindResponse.setRespDesc(e.getMessage());
            } else {
                mchntUnBindResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntUnBindResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntUnBindResponse);
        return bspResult;
    }

    @Override
    public Result<ManagerAuthResponse> managerAuth(ManagerAuthRequest managerAuthRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ManagerAuthResponse> bspResult = new Result<>();
        ManagerAuthResponse managerAuthResponse = new ManagerAuthResponse();
        managerAuthResponse.setHeader(managerAuthRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(managerAuthRequest, ManagerAuthResponse.class);
            managerAuthResponse = (ManagerAuthResponse) dubboResult.getResult();
            managerAuthResponse.setRespCode(dubboResult.getRespCode());
            managerAuthResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("拓展经理认证登录失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                managerAuthResponse.setRespCode(((BizException) e).getCode());
                managerAuthResponse.setRespDesc(e.getMessage());
            } else {
                managerAuthResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                managerAuthResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(managerAuthResponse);
        return bspResult;
    }

    @Override
    public Result<MchntExpandAcceptResponse> mchntExpandAccept(MchntExpandAcceptRequest mchntExpandAcceptRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntExpandAcceptResponse> bspResult = new Result<>();
        MchntExpandAcceptResponse mchntExpandAcceptResponse = new MchntExpandAcceptResponse();
        mchntExpandAcceptResponse.setHeader(mchntExpandAcceptRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntExpandAcceptRequest, MchntExpandAcceptResponse.class);
            mchntExpandAcceptResponse = (MchntExpandAcceptResponse) dubboResult.getResult();
            mchntExpandAcceptResponse.setRespCode(dubboResult.getRespCode());
            mchntExpandAcceptResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("拓展经理受理商户失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntExpandAcceptResponse.setRespCode(((BizException) e).getCode());
                mchntExpandAcceptResponse.setRespDesc(e.getMessage());
            } else {
                mchntExpandAcceptResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntExpandAcceptResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntExpandAcceptResponse);
        return bspResult;
    }

    @Override
    public Result<EventProcessResponse> eventProcess(EventProcessRequest eventProcessRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<EventProcessResponse> bspResult = new Result<>();
        EventProcessResponse eventProcessResponse = new EventProcessResponse();
        eventProcessResponse.setHeader(eventProcessRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(eventProcessRequest, EventProcessResponse.class);
            eventProcessResponse = (EventProcessResponse) dubboResult.getResult();
            eventProcessResponse.setRespCode(dubboResult.getRespCode());
            eventProcessResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("工单处理失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                eventProcessResponse.setRespCode(((BizException) e).getCode());
                eventProcessResponse.setRespDesc(e.getMessage());
            } else {
                eventProcessResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                eventProcessResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(eventProcessResponse);
        return bspResult;
    }

    @Override
    public Result<ReconDetailDownloadResponse> reconDetailDownload(ReconDetailDownloadRequest reconDetailDownloadRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<ReconDetailDownloadResponse> bspResult = new Result<>();
        ReconDetailDownloadResponse reconDetailDownloadResponse = new ReconDetailDownloadResponse();
        reconDetailDownloadResponse.setHeader(reconDetailDownloadRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(reconDetailDownloadRequest, ReconDetailDownloadResponse.class);
            reconDetailDownloadResponse = (ReconDetailDownloadResponse) dubboResult.getResult();
            reconDetailDownloadResponse.setRespCode(dubboResult.getRespCode());
            reconDetailDownloadResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("对账单下载失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                reconDetailDownloadResponse.setRespCode(((BizException) e).getCode());
                reconDetailDownloadResponse.setRespDesc(e.getMessage());
            } else {
                reconDetailDownloadResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                reconDetailDownloadResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(reconDetailDownloadResponse);
        return bspResult;
    }

    @Override
    public Result<MchntCashWithdrawalResponse> mchntCashWithdrawal(MchntCashWithdrawalRequest mchntCashWithdrawalRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntCashWithdrawalResponse> bspResult = new Result<>();
        MchntCashWithdrawalResponse mchntCashWithdrawalResponse = new MchntCashWithdrawalResponse();
        mchntCashWithdrawalResponse.setHeader(mchntCashWithdrawalRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(mchntCashWithdrawalRequest, MchntCashWithdrawalResponse.class);
            mchntCashWithdrawalResponse = (MchntCashWithdrawalResponse) dubboResult.getResult();
            mchntCashWithdrawalResponse.setRespCode(dubboResult.getRespCode());
            mchntCashWithdrawalResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户提现失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                mchntCashWithdrawalResponse.setRespCode(((BizException) e).getCode());
                mchntCashWithdrawalResponse.setRespDesc(e.getMessage());
            } else {
                mchntCashWithdrawalResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntCashWithdrawalResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(mchntCashWithdrawalResponse);
        return bspResult;
    }

    @Override
    public Result<VoiceDevBindOrUnBindResponse> voiceDevBindOrUnBind(VoiceDevBindOrUnBindRequest voiceDevBindOrUnBindRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<VoiceDevBindOrUnBindResponse> bspResult = new Result<>();
        VoiceDevBindOrUnBindResponse voiceDevBindOrUnBindResponse = new VoiceDevBindOrUnBindResponse();
        voiceDevBindOrUnBindResponse.setHeader(voiceDevBindOrUnBindRequest.getHeader());
        try {
            DubboResult dubboResult = dubboService.callBizService(voiceDevBindOrUnBindRequest, VoiceDevBindOrUnBindResponse.class);
            voiceDevBindOrUnBindResponse = (VoiceDevBindOrUnBindResponse) dubboResult.getResult();
            voiceDevBindOrUnBindResponse.setRespCode(dubboResult.getRespCode());
            voiceDevBindOrUnBindResponse.setRespDesc(dubboResult.getRespDesc());
            bspResult.setSuccess(dubboResult.isSuccess());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("语音设备绑定解绑失败:<{}>", byteArrayOutputStream.toString());
            bspResult.setSuccess(false);
            if (e instanceof BizException) {
                voiceDevBindOrUnBindResponse.setRespCode(((BizException) e).getCode());
                voiceDevBindOrUnBindResponse.setRespDesc(e.getMessage());
            } else {
                voiceDevBindOrUnBindResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                voiceDevBindOrUnBindResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        bspResult.setResult(voiceDevBindOrUnBindResponse);
        return bspResult;
    }
}
